package com.jy.api.util;

import com.jy.api.domain.PaymentChannel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by lihu on 2017/10/7.
 */
@Component
public class Payutils {

    @Value("${api.gateway.url}")
    private String apiGatewayUrl;




    public static final String ALI_QR_CODE = "https://pay.hstypay.com/v2/pay/gateway";

    //支付宝回调接口
    public static final String NOTIFY_URL = "APIGATEWAYURL/v1/pay/callBack";

    //支付宝回调接口
    public static final String STORE_NOTIFY_URL = "APIGATEWAYURL/v1/pay/store/callBack";

    //店铺充值回调接口
    public static final String RECHARGE_NOTIFY_URL = "APIGATEWAYURL/v1/pay/recharge/callBack";


    /**
     * 获取店铺等级收费回调
     *
     * @return
     */
    public String getStoreRecharge() {
        return RECHARGE_NOTIFY_URL.replace("APIGATEWAYURL", apiGatewayUrl);
    }

    /**
     * @return
     */
    public String getNotifyUrl() {

        return NOTIFY_URL.replace("APIGATEWAYURL", apiGatewayUrl);
    }

    /**
     * 店铺支付回调二维码（用于其他费用类支付）
     *
     * @return
     */
    public String getStoreNotifyUrl() {

        return STORE_NOTIFY_URL.replace("APIGATEWAYURL", apiGatewayUrl);
    }

    public static PaymentChannel getPaymentChannel(String tradeType) {
        PaymentChannel paymentChannel = null;
        if (StringUtils.startsWith(tradeType,"pay.weixin")) {
            paymentChannel = PaymentChannel.WECHATPAY;
        } else if (StringUtils.startsWith(tradeType,"pay.alipay")) {
            paymentChannel = PaymentChannel.ALIPAY;
        } else if(StringUtils.startsWith(tradeType,"pay.unionpay")) {
            paymentChannel = PaymentChannel.UNIONPAY;
        }else{
            throw new RuntimeException("Unknown trade type:" + tradeType);
        }
        return paymentChannel;
    }
}
